﻿using ServerCommon.Config;
using ServerCommon.Log;
using ServerCommon.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GMServer.Server
{
    public class GMNet
    {
        public static readonly GMNet Instance = new Lazy<GMNet>(() => new GMNet(), true).Value;
        private static LogHelper log = LogHelper.GetLogger(typeof(GMNet));

        NetNodeManager _managerGM;
        NetService _serviceGM;

        public void Init()
        {
            _managerGM = new NetNodeManager();
            _managerGM.Init();

            var netConfig = ConfigManager.Instance.NetConfig["NetConfigure"];
            var gmNetConfig = netConfig["GMNetConfig"];
            var serviceNetConfig = gmNetConfig["GameService"];
            int connectionCount = serviceNetConfig.Value<int>("MaxConnection");
            int bufferSize = serviceNetConfig.Value<int>("SAEABufferSize");

            var serverConfig = ConfigManager.Instance.ServerConfig;
            var gmConfig = serverConfig["GMServer"];
            string ip = gmConfig.Value<string>("Ip");
            int port = gmConfig.Value<int>("Port");
            long localServerId = gmConfig.Value<long>("Id");

            _serviceGM = new NetService();
            _serviceGM.Config(_managerGM, EnumNodeServiceType.Database, ip, port, connectionCount, bufferSize, localServerId);
            _serviceGM.Start();
            _serviceGM.ShowPrompt();
        }

        public void Run()
        {
            _managerGM.DoNetMessages();
        }

        public void Stop()
        {
            _managerGM.StopNetwork();
        }

        public void Release()
        {
        }
    }
}
